/*
 * CKeyboardControl.cpp
 *
 *  Created on: 05.10.2012
 *      Author: Caffeine
 */

#include "CKeyboardControl.h"
#include <Keycodes.h>
#include <log/Log.h>

namespace irr{
namespace control{


CKeyboardControl::CKeyboardControl(){
	moveableObjects=0;
	key_move.setKey(irr::KEY_KEY_W);
	key_move_back.setKey(irr::KEY_KEY_S);
	key_shift_left.setKey(irr::KEY_KEY_A);
	key_shift_right.setKey(irr::KEY_KEY_D);
}

CKeyboardControl::~CKeyboardControl(){
	setMoveableObjects(0);
}

void CKeyboardControl::setMoveableObjects(irr::object::MoveableObjects*object){
	if (moveableObjects==object)
		return;
	if (moveableObjects)
		moveableObjects->drop();
	moveableObjects=object;
	if (moveableObjects)
		moveableObjects->grab();
}
irr::object::MoveableObjects*CKeyboardControl::getMoveableObjects(){
	return moveableObjects;
}
void CKeyboardControl::step(irr::f32 deltatime){
	if (!moveableObjects)
		return;

	if (key_move.isDown()){
		moveableObjects->move(deltatime);
	}

	if (key_move_back.isDown())
		moveableObjects->moveBack(deltatime);

	if (key_shift_left.isDown())
		moveableObjects->shiftLeft(deltatime);

	if (key_shift_right.isDown())
		moveableObjects->shiftRight(deltatime);
}

bool CKeyboardControl::keyDown(irr::EKEY_CODE key){
	if (key_move.getKey()==key){
		if (!key_move.isDown() && moveableObjects)
			moveableObjects->onChangeMoveStatus(true);
		key_move.setDown(true);
		return true;
	}

	if (key_move_back.getKey()==key){
		if (!key_move_back.isDown() && moveableObjects)
			moveableObjects->onChangeMoveBackStatus(true);
		key_move_back.setDown(true);
		return true;
	}

	if (key_shift_left.getKey()==key){
		if (!key_shift_left.isDown() && moveableObjects)
			moveableObjects->onChangeShiftLeftStatus(true);
		key_shift_left.setDown(true);
		return true;
	}

	if (key_shift_right.getKey()==key){
		if (!key_shift_right.isDown() && moveableObjects)
			moveableObjects->onChangeShiftRightStatus(true);
		key_shift_right.setDown(true);
		return true;
	}
	return false;
}

bool CKeyboardControl::keyUp(irr::EKEY_CODE key){
	if (key_move.getKey()==key){
		if (key_move.isDown() && moveableObjects)
			moveableObjects->onChangeMoveStatus(false);
		key_move.setDown(false);
		return true;
	}

	if (key_move_back.getKey()==key){
		if (key_move_back.isDown() && moveableObjects)
			moveableObjects->onChangeMoveBackStatus(false);
		key_move_back.setDown(false);
		return true;
	}

	if (key_shift_left.getKey()==key){
		if (key_shift_left.isDown() && moveableObjects)
			moveableObjects->onChangeShiftLeftStatus(false);
		key_shift_left.setDown(false);
		return true;
	}

	if (key_shift_right.getKey()==key){
		if (key_shift_right.isDown() && moveableObjects)
			moveableObjects->onChangeShiftRightStatus(false);
		key_shift_right.setDown(false);
		return false;
	}
	return false;
}

void CKeyboardControl::setBindKey(irr::EKEY_CODE key,KeyBindType type){
	switch (type){
		case KBT_MOVE:
			key_move.setKey(key);
			return;
		case KBT_MOVE_BACK:
			key_move_back.setKey(key);
			return;
		case KBT_SHIFT_LEFT:
			key_shift_left.setKey(key);
			return;
		case KBT_SHIFT_RIGHT:
			key_shift_right.setKey(key);
			return;
	}
}

char CKeyboardControl::getBindKey(KeyBindType type){
	switch (type){
		case KBT_MOVE:
			return key_move.getKey();
		case KBT_MOVE_BACK:
			return key_move_back.getKey();
		case KBT_SHIFT_LEFT:
			return key_shift_left.getKey();
		case KBT_SHIFT_RIGHT:
			return key_shift_right.getKey();
	}
	return char(0);
}

bool CKeyboardControl::isKeyDown(KeyBindType type){
	switch (type){
		case KBT_MOVE:
			return key_move.isDown();
		case KBT_MOVE_BACK:
			return key_move_back.isDown();
		case KBT_SHIFT_LEFT:
			return key_shift_left.isDown();
		case KBT_SHIFT_RIGHT:
			return key_shift_right.isDown();
	}
	return false;
}

}//namespace control
}//namespace irr
